//
//  KLScannerView.m
//  KLBookStore
//
//  Created by apple on 2017/10/10.
//  Copyright © 2017年 NetEaseExam. All rights reserved.
//

#import "KLScannerView.h"

static const CGFloat kScannerLineWidth = 1.0f;

@interface KLScannerView ()

@property (nonatomic, assign) CGSize rectSize;//扫描区域大小

@property (nonatomic, assign) CGFloat offsetY;//扫描区域相对于view中心点的偏移量

@property (nonatomic, strong) UIImageView *scannerLine;//扫描线

@property (nonatomic, assign, getter=isAnimationReverse) BOOL animationReverse;//是否可以反向扫描

@property (nonatomic, assign, getter=isAnimating) BOOL animating;

@property (nonatomic, assign) CGFloat minX;
@property (nonatomic, assign) CGFloat minY;
@property (nonatomic, assign) CGFloat maxX;
@property (nonatomic, assign) CGFloat maxY;

@end

@implementation KLScannerView

- (instancetype)initWithFrame:(CGRect)frame rectSize:(CGSize)rectSize offsetY:(CGFloat)offsetY{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.rectSize = rectSize;
        self.offsetY = offsetY;
        
        self.minX = (self.frame.size.width - self.rectSize.width) * 0.5;
        self.maxX = self.minX + self.rectSize.width;
        self.minY = (self.frame.size.height - self.rectSize.height) * 0.5 + self.offsetY;
        self.maxY = self.minY + self.rectSize.height;
    }
    return self;
}

- (UIImageView *)scannerLine{
    if (_scannerLine == nil) {
        _scannerLine = [[UIImageView alloc] initWithFrame:CGRectMake(_minX, _minY, self.rectSize.width, kScannerLineWidth)];
        _scannerLine.image = [UIImage imageNamed:@"scanner-line"];
        
        [self addSubview:_scannerLine];
    }
    return _scannerLine;
}

- (void)startAnimation{
    if (self.isAnimating) {
        return;
    }
    self.animating = YES;

    [UIView animateWithDuration:3.0f delay:0.5f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        if (self.animationReverse) {
            self.scannerLine.frame = CGRectMake(_minX, _minY, self.rectSize.width, kScannerLineWidth);
        }else{
            self.scannerLine.frame = CGRectMake(_minX, _maxY, self.rectSize.width, kScannerLineWidth);
        }
    } completion:^(BOOL finished) {
        if (finished) {
            self.animating = NO;
            self.animationReverse = !self.animationReverse;
            [self startAnimation];
        } else {
            [self stopAnimation];
        }
    }];
}

- (void)stopAnimation{
    [self.scannerLine removeFromSuperview];
    self.scannerLine = nil;
    self.animationReverse = NO;
    self.animating = NO;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //1.up left down right coverRect
    CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.4);
    CGContextFillRect(ctx, CGRectMake(0, 0, self.frame.size.width, _minY));
    CGContextFillRect(ctx, CGRectMake(0, _minY, _minX, self.rectSize.height));
    CGContextFillRect(ctx, CGRectMake(0, _maxY, self.frame.size.width, self.frame.size.height - _maxY));
    CGContextFillRect(ctx, CGRectMake(_maxX, _minY, _minX, self.rectSize.height));
    
    //2.white border
    CGFloat borderLineThick = 0.5f;
    CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(ctx, borderLineThick);
    CGContextAddRect(ctx, CGRectMake(_minX, _minY, self.rectSize.width, self.rectSize.height));
    CGContextStrokePath(ctx);
    
    //3.white corner
    CGFloat cornerLineLength = 9.0f;
    CGFloat cornerLineThick = 1.0f;
    
    CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(ctx, cornerLineThick);
    
    //up-left corner
    CGContextMoveToPoint(ctx, _minX + cornerLineLength - cornerLineThick, _minY - cornerLineThick);
    CGContextAddLineToPoint(ctx, _minX - cornerLineThick, _minY - cornerLineThick);
    CGContextAddLineToPoint(ctx, _minX - cornerLineThick, _minY - cornerLineThick + cornerLineLength);
    //down-left corner
    CGContextMoveToPoint(ctx, _minX + cornerLineLength - cornerLineThick, _maxY + cornerLineThick);
    CGContextAddLineToPoint(ctx, _minX - cornerLineThick, _maxY + cornerLineThick);
    CGContextAddLineToPoint(ctx, _minX - cornerLineThick, _maxY + cornerLineThick - cornerLineLength);
    //up-right corner
    CGContextMoveToPoint(ctx, _maxX - cornerLineLength + cornerLineThick, _minY - cornerLineThick);
    CGContextAddLineToPoint(ctx, _maxX + cornerLineThick, _minY - cornerLineThick);
    CGContextAddLineToPoint(ctx, _maxX + cornerLineThick, _minY - cornerLineThick + cornerLineLength);
    //down-right corner
    CGContextMoveToPoint(ctx, _maxX - cornerLineLength + cornerLineThick, _maxY + cornerLineThick);
    CGContextAddLineToPoint(ctx, _maxX + cornerLineThick, _maxY + cornerLineThick);
    CGContextAddLineToPoint(ctx, _maxX + cornerLineThick, _maxY + cornerLineThick - cornerLineLength);
    
    CGContextStrokePath(ctx);

}


@end
